#Andrew Gooch
#April 11, 2017
#Ripping Yarn, Political Communications
#Replication R file 6: Figure 3 Social Security support plot

#This R file creates Figure 3 using ggplot.

#REQUIREMENTS:
#Must run May_weights.R before running code below


#AVERAGE TREATMENT EFFECTS# REFERENCED IN TEXT
svymean(~SuppCont,design=May15.svy.rake, na.rm=TRUE)
confint(svymean(~SuppCont,design=May15.svy.rake, na.rm=TRUE))
svymean(~SuppCue,design=May15.svy.rake, na.rm=TRUE)
confint(svymean(~SuppCue,design=May15.svy.rake, na.rm=TRUE))
svymean(~SuppAnonStory,design=May15.svy.rake, na.rm=TRUE)
confint(svymean(~SuppAnonStory,design=May15.svy.rake, na.rm=TRUE))


##########################################
# mean support and confidence intervals MARCH
##########################################

demsub <- subset(May15.svy.rake, PID3_lean_new==1)

demsControl_mean_May15 <- svymean(~SuppCont,design=demsub, na.rm=TRUE)[1]
demsControl_lower_May15 <- confint(svymean(~SuppCont,design=demsub, na.rm=TRUE))[1]
demsControl_upper_May15 <- confint(svymean(~SuppCont,design=demsub, na.rm=TRUE))[2]

demsCue_mean_May15 <- svymean(~SuppCue,design=demsub, na.rm=TRUE)[1]
demsCue_lower_May15 <- confint(svymean(~SuppCue,design=demsub, na.rm=TRUE))[1]
demsCue_upper_May15 <- confint(svymean(~SuppCue,design=demsub, na.rm=TRUE))[2]

demsAnonStory_mean_May15 <- svymean(~SuppAnonStory,design=demsub, na.rm=TRUE)[1]
demsAnonStory_lower_May15 <- confint(svymean(~SuppAnonStory,design=demsub, na.rm=TRUE))[1]
demsAnonStory_upper_May15 <- confint(svymean(~SuppAnonStory,design=demsub, na.rm=TRUE))[2]

indsub <- subset(May15.svy.rake, PID3_lean_new==0)

indsControl_mean_May15 <- svymean(~SuppCont,design=indsub, na.rm=TRUE)[1]
indsControl_lower_May15 <- confint(svymean(~SuppCont,design=indsub, na.rm=TRUE))[1]
indsControl_upper_May15 <- confint(svymean(~SuppCont,design=indsub, na.rm=TRUE))[2]

indsCue_mean_May15 <- svymean(~SuppCue,design=indsub, na.rm=TRUE)[1]
indsCue_lower_May15 <- confint(svymean(~SuppCue,design=indsub, na.rm=TRUE))[1]
indsCue_upper_May15 <- confint(svymean(~SuppCue,design=indsub, na.rm=TRUE))[2]

indsAnonStory_mean_May15 <- svymean(~SuppAnonStory,design=indsub, na.rm=TRUE)[1]
indsAnonStory_lower_May15 <- confint(svymean(~SuppAnonStory,design=indsub, na.rm=TRUE))[1]
indsAnonStory_upper_May15 <- confint(svymean(~SuppAnonStory,design=indsub, na.rm=TRUE))[2]

repsub <- subset(May15.svy.rake, PID3_lean_new==-1)

repsControl_mean_May15 <- svymean(~SuppCont,design=repsub, na.rm=TRUE)[1]
repsControl_lower_May15 <- confint(svymean(~SuppCont,design=repsub, na.rm=TRUE))[1]
repsControl_upper_May15 <- confint(svymean(~SuppCont,design=repsub, na.rm=TRUE))[2]

repsCue_mean_May15 <- svymean(~SuppCue,design=repsub, na.rm=TRUE)[1]
repsCue_lower_May15 <- confint(svymean(~SuppCue,design=repsub, na.rm=TRUE))[1]
repsCue_upper_May15 <- confint(svymean(~SuppCue,design=repsub, na.rm=TRUE))[2]

repsAnonStory_mean_May15 <- svymean(~SuppAnonStory,design=repsub, na.rm=TRUE)[1]
repsAnonStory_lower_May15 <- confint(svymean(~SuppAnonStory,design=repsub, na.rm=TRUE))[1]
repsAnonStory_upper_May15 <- confint(svymean(~SuppAnonStory,design=repsub, na.rm=TRUE))[2]


##########################################
# 3-panel plot, one for each PID, MARCH
##########################################

#first panel plot -

coef <- c(demsControl_mean_May15,
          demsCue_mean_May15,
          demsAnonStory_mean_May15)

lb <-c(demsControl_lower_May15,
       demsCue_lower_May15,
       demsAnonStory_lower_May15)

ub <-c(demsControl_upper_May15,
       demsCue_upper_May15,
       demsAnonStory_upper_May15)


method <-c("Control Group",
           "Cue Only Group",
           "Impersonalized Story Group")


specification <-c(1,2,3)


df <- data.frame(coef, method, specification,lb,ub)

p1 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("blue","blue","blue")) + 
  scale_shape_manual(name=" ", values=c(17,19,12)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Democrats") +
  scale_y_continuous("Support for Social Security Reform (0-1)", limits=c(0.45,.85)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p1)


#second panel plot - 

coef <- c(indsControl_mean_May15,
          indsCue_mean_May15,
          indsAnonStory_mean_May15)

lb <-c(indsControl_lower_May15,
       indsCue_lower_May15,
       indsAnonStory_lower_May15)

ub <-c(indsControl_upper_May15,
       indsCue_upper_May15,
       indsAnonStory_upper_May15)


method <-c("Control Group",
           "Cue Only Group",
           "Impersonalized Story Group")


specification <-c(1,2,3)


df <- data.frame(coef, method, specification,lb,ub)

p2 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("grey","grey","grey")) + 
  scale_shape_manual(name=" ", values=c(17,19,12)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Independents") +
  scale_y_continuous(" ", limits=c(0.45,.85)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p2)

coef <- c(repsControl_mean_May15,
          repsCue_mean_May15,
          repsAnonStory_mean_May15)

lb <-c(repsControl_lower_May15,
       repsCue_lower_May15,
       repsAnonStory_lower_May15)

ub <-c(repsControl_upper_May15,
       repsCue_upper_May15,
       repsAnonStory_upper_May15)


method <-c("Control Group",
           "Cue Only Group",
           "Impersonalized Story Group")


specification <-c(1,2,3)


df <- data.frame(coef, method, specification,lb,ub)

p3 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("red","red", "red")) + 
  scale_shape_manual(name=" ", values=c(17,19,12)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Republicans") +
  scale_y_continuous(" ", limits=c(0.45,.85)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p3)


g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(p2)

p4 <- grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                               p2 + theme(legend.position="none"),
                               p3 + theme(legend.position="none"),
                               nrow=1),
                   mylegend, nrow=2,heights=c(10, 1))




